﻿
using Models.JsonResultModel;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Text.RegularExpressions;
using UtilsBase;

namespace ServiceWeb.WebClient
{
    public class ServiceWifis
    {

        public List<string> getLinuxWifiList()
        {
            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "/bin/bash",
                    Arguments = "-c \"iw dev wlan0 scan | grep SSID\"",
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = true
                }
            };

            process.Start();
            string result = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            // Console.WriteLine(result);
            result = result.Replace("\\x00", "");
            List<string> wifis = new List<string>();

            MatchCollection matches = Regex.Matches(result, @"(?<=SSID: ).*");
            foreach (Match match in matches)
            {
                if (!string.IsNullOrEmpty(match.Value))
                {
                    if (wifis.Where(c => c == match.Value).Count() < 1)
                    {
                        wifis.Add(match.Value);
                    }
                }
                Console.WriteLine("Found 'ai' at position " + match.Value);
            }
            return wifis;

        }
        public List<string> getLinuxWifiLink()
        {
            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "/bin/bash",
                    Arguments = "-c \"iw dev wlan0 link\"",
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = true
                }
            };

            process.Start();
            string result = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            // Console.WriteLine(result);
            result = result.Replace("\\x00", "");
            List<string> wifis = new List<string>();

            MatchCollection matches = Regex.Matches(result, @"(?<=SSID: ).*");
            foreach (Match match in matches)
            {
                if (!string.IsNullOrEmpty(match.Value))
                {
                    if (wifis.Where(c => c == match.Value).Count() < 1)
                    {
                        wifis.Add(match.Value.Trim());
                    }
                }
                Console.WriteLine("Found 'ai' at position " + match.Value.Trim());
            }
            return wifis;

        }
        public List<string> setLinuxWifiContent(string ssid,string key)
        {
            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "/bin/bash",
                    Arguments = "nmcli device wifi connect \"" + ssid + "\" password \"" + key + "\"",
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = true
                }
            };

            process.Start();
            string result = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            // Console.WriteLine(result);
            result = result.Replace("\\x00", "");
            List<string> wifis = new List<string>();

            MatchCollection matches = Regex.Matches(result, @"(?<=SSID: ).*");
            foreach (Match match in matches)
            {
                if (!string.IsNullOrEmpty(match.Value))
                {
                    if (wifis.Where(c => c == match.Value).Count() < 1)
                    {
                        wifis.Add(match.Value.Trim());
                    }
                }
                Console.WriteLine("Found 'ai' at position " + match.Value.Trim());
            }
            return wifis;

        }
    }
}
